﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using Microsoft.JSInterop.Implementation;
using ZhiJiao.Dal;
using ZhiJiao.Service.Abstracts;
using ZhiJiao.Service.Dto;

namespace Step4.Unit7.EFCore.Controllers
{
    /// <summary>
    /// 测试
    /// </summary>
    public class EvaluationController : Controller
    {
        private readonly ICurrentUser _currentUser;
        private readonly ZJDbContext _db;
        private readonly IEvaluationService _evaluationService;

        public EvaluationController(ICurrentUser currentUser,
                                    ZJDbContext zJDbContext,
                                    IEvaluationService ealuationService)
        {
            _currentUser = currentUser;
            _db = zJDbContext;
            _evaluationService = ealuationService;
        }

        /// <summary>
        /// 测试主视图
        /// </summary>
        public IActionResult Index()
        {
            ViewBag.UserType = Convert.ToInt32(_currentUser.User.Type);
            ViewBag.InsId = _currentUser.User.Instruction!.Id;
            return View();
        }

        /// <summary>
        /// 添加测评视图
        /// </summary>
        public IActionResult Add()
        {
            ViewBag.ActionType = Request.Query["actionType"].ToString();
            ViewBag.Id = -1;
            if (ViewBag.ActionType == "edit")
            {
                ViewBag.Id = Convert.ToInt32(Request.Query["id"]);
            } 
            ViewBag.UserType = Convert.ToInt32(_currentUser.User.Type);
            ViewBag.InsId = _currentUser.User.Instruction!.Id;
            ViewBag.HostWithScheme = $"{Request.Scheme}://{Request.Host.Value}";
            return View();
        }

        /// <summary>
        ///  获取机构测评列表
        /// </summary>
        [HttpGet("api/evaluation/get-ins-evaluation-list")]
        public async Task<IActionResult> GetEvaList(string?  filterName) 
        {
            return Json( await _evaluationService.GetEvaluationList(filterName));
        }

        /// <summary>
        /// 保存测评
        /// </summary>
        [HttpPost("api/evaluation/save")]
        public async Task<IActionResult> Save([FromBody]EvaluationDto dto)
        {
            return Json(await _evaluationService.Save(dto));
        }

        /// <summary>
        ///  提交
        /// </summary>
        [HttpPost("api/evaluation/submit")]
        public async Task<IActionResult> Submit([FromBody] EvaluationDto dto)
        {
            return Json(await _evaluationService.Submit(dto));
        }

        /// <summary>
        /// 新建
        /// </summary>
        [HttpGet("api/evaluation/create-new-evaluation")]
        public async Task<IActionResult> CreateNewEvaluation()
        {
            return Json(await _evaluationService.CreateEvaluation());
        }

        /// <summary>
        /// 删除测评
        /// </summary>
        [HttpGet("api/evaluation/delete-evalution")]
        public async Task<IActionResult> DeleteEvaluation(long id)
        {
            return Json(await _evaluationService.DeleteEvaluationg(id));
        }

        /// <summary>
        /// 获取测评
        /// </summary>
        [HttpGet("api/evaluation/get-eval")]
        public async Task<IActionResult> GetEval(long id)
        {
            return Json(await _evaluationService.GetEval(id));
        }

        /// <summary>
        ///  上传ListImage
        /// </summary>
        [HttpPost("api/evaluation/upload-list-image")]
        public async Task<IActionResult> UploadListImage(long evalId)
        {
            return Json(await _evaluationService.UploadImage(evalId, "listIamge"));
        }

        /// <summary>
        /// 上传mainIamge
        /// </summary>
        [HttpPost("api/evaluation/upload-main-image")]
        public async Task<IActionResult> UploadMain(long evalId)
        {
            return Json(await _evaluationService.UploadImage(evalId, "mainImage"));
        }

        /// <summary>
        /// 上传详情图片
        /// </summary>
        [HttpPost("api/evaluation/upload-detail-images")]
        public async Task<IActionResult> UploadDetail(long evalId)
        {
            return Json(await _evaluationService.UploadImage(evalId, "detailIamges"));
        }


        /// <summary>
        /// 删除图片
        /// </summary>
        [HttpGet("api/evaluation/remove-image")]
        public async Task<IActionResult> RemoveImage(long id,string actionType)
        {
            return Json(await _evaluationService.DeleteImageAsync(id, actionType));
        }


        /// <summary>
        /// 获取模块列表
        /// </summary>
        [HttpGet("api/evaluation/get-evaluation-parts")]
        public async Task<IActionResult> GetEvaluationParts(long id)
        {
            return Json(await _evaluationService.GetEvaluationParts(id));
        }

        /// <summary>
        /// 获取测试模块
        /// </summary>
        [HttpGet("api/evaluation/get-eval-part")]
        public async Task<IActionResult> GetEvalPart(long id)
        {
            var response = await _evaluationService.GetEvalPart(id);
            return Json(response);
        }

        /// <summary>
        /// 保存测评模块
        /// </summary>
        [HttpPost("api/evaluation/save-part")]
        public async Task<IActionResult> SaveEvalPart( [FromBody]EvaluationPartDto part,[FromQuery]string actionType)
        {
            var response = await _evaluationService.SavePart(part, actionType);
            return Json(response);
        }

        // 删除模块
        [HttpGet("api/evaluation/delete-evaluation-part")]
        public async Task<IActionResult> DeletePart(long id)
        {
            return Ok(await _evaluationService.DeletePart(id));
        }

        // 获取模块测试题
        [HttpGet("api/evaluation/get-part-question-list")]
        public async Task<IActionResult> GetPartQuestions(long partId)
        {
            return Json(await _evaluationService.GetPartQuestions(partId));
        }


        /// <summary>
        /// 获取测试题
        /// </summary>
        [HttpGet("api/evaluation/get-question")]
        public async Task<IActionResult> GetQuestion(long id)
        {
            return Json(await _evaluationService.GetQuestion(id));
        }


        /// <summary>
        /// 保存测试题
        /// </summary>
        [HttpPost("api/evaluation/save-question")]
        public async Task<IActionResult> SaveQuestion([FromBody]QuestionDto question,string actionType)
        {
            return Json(await _evaluationService.SaveQuestion(question,actionType));
        }

        /// <summary>
        ///  删除测试题
        /// </summary>
        [HttpGet("api/evaluation/delete-question")]
        public async Task<IActionResult> DeleteQuestion(long id)
        {
            return Json(await _evaluationService.DeleteQuestion(id));
        }

        // 获取测试题选项集合
        [HttpGet("api/evaluation/get-question-options")]
        public async Task<IActionResult> GetQuestionOptions(long questionId)
        {
            return Json(await _evaluationService.GetQuestionOptions(questionId));
        }

        // 获取选项
        [HttpGet("api/evaluation/get-option")]
        public async Task<IActionResult> GetOption(long id)
        {
            return Json(await _evaluationService.GetOption(id));
        }


        /// <summary>
        /// 保存选项
        /// </summary>
        [HttpPost("api/evaluation/save-option")]
        public async Task<IActionResult> SaveOption([FromBody] OptionDto dto)
        {
            return Json(await _evaluationService.SaveOption(dto));
        }

        /// <summary>
        /// 删除选项
        /// </summary>
        [HttpGet("api/evaluation/delete-option")]
        public async Task<IActionResult> DeleteOption(long id)
        {
            return Json(await _evaluationService.DeleteOption(id));
        }

        /// <summary>
        /// 生成选项标签
        /// </summary>
        [HttpGet("api/evaluation/generate-label")]
        public async Task<IActionResult> GenerateLabel(long questionId)
        {
            return Json(await _evaluationService.GenerateOptionLabel(questionId));
        }

        /// <summary>
        /// 保存测试题答案
        /// </summary>
        [HttpPost("api/evaluation/save-quesion-result")]
        public async Task<IActionResult> SaveQuestionReult([FromQuery]long questionId,[FromBody]QuestionResultDto result)
        {
            return Json(await _evaluationService.SaveQuestionResult(questionId,result.Ids));
        }

        /// <summary>
        /// 获取机构参与规则列表
        /// </summary>
        [HttpGet("api/evaluation/get-ins-rules")]
        public async Task<IActionResult> GetInsRules(long insId,long evalId)
        {
            return Json(await _evaluationService.GetInsRules(insId, evalId));
        }

        /// <summary>
        /// 从测评中移除参与规则
        /// </summary>
        [HttpGet("api/evaluation/remove-rule-from-eval")]
        public async Task<IActionResult> RemoveRuleFromEval(long evalId,long ruleId)
        {
            return Json(await _evaluationService.RemoveRuleFromEval(evalId,ruleId));
        }

        /// <summary>
        /// 将参与7规则加入测评
        /// </summary>
        [HttpGet("api/evaluation/add-rule-to-eval")]
        public async Task<IActionResult> AddRuleToEval(long evalId, long ruleId)
        {
            return Json(await _evaluationService.AddRuleToEval(evalId,ruleId));
        }
    }
}
